/**
 * Copyright © 2011 looty - Textoo
 * @link http://www.textoo.org
 * @link http://code.google.com/p/textoo/
 * @author lab9 - Bertrand Larrieu
 * @mail lab9.fr@gmail.com
 * @version 2.0
 */

package org.looty.core.textoo.render.glyph 
{
	import flash.display.*;
	import flash.geom.*;
	import flash.text.*;
	import flash.utils.*;
	import org.looty.log.*;
	
	public class GlyphTable
	{
		
		private var _table			:Dictionary;		
		private var _textField		:TextField;	
		private var _textFormat		:TextFormat;
		private var _container		:Sprite;		
		private var _font			:Font;		
		private var _isEmbed		:Boolean;
		private var _useDeviceFonts	:Boolean;
		private var _textColorType	:String;
		
		private var _mask			:uint;
		private var _color			:uint;
		private var _matrix			:Matrix;		
		
		public function GlyphTable(textFormat:TextFormat, font:Font, textColorType:String, useDeviceFonts:Boolean, sharpness:Number = 0, thickness:Number = 0) 
		{
			_font = font;			
			_isEmbed = _font != null;	
			_useDeviceFonts = useDeviceFonts;
			
			_table = new Dictionary();			
			
			_textField = new TextField();
			_textField.selectable = false;			
			_textField.antiAliasType = AntiAliasType.ADVANCED;
			_textField.gridFitType = GridFitType.PIXEL;		
			_textField.sharpness = sharpness;
			_textField.thickness = thickness;
			_textField.autoSize = TextFieldAutoSize.LEFT;
			
			_color = textColorType == TextColorType.LIGHT_COLOR ? 0xFFFFFF : 0x000000;
			_mask = 0xFF << 24 + _color;
			
			_matrix = new Matrix();
			
			_textFormat = new TextFormat(textFormat.font, textFormat.size, _color , Boolean(textFormat.bold), Boolean(textFormat.italic), Boolean(textFormat.underline), null, null, TextFormatAlign.LEFT);
			_textField.defaultTextFormat = _textFormat;					
			
			_container = new Sprite();
			_container.addChild(_textField);
		}
		
		
		private function createGlyph(value:String):void
		{
			if (!_isEmbed && !_useDeviceFonts)
			{
				Looger.error("no font embeded at character creation of " +  (_font != null ? _font.fontName + " " + _font.fontStyle : "") + " U+" + value.charCodeAt().toString(16));
				_table[value] = null;
				return;
			}
			_textField.autoSize = TextFieldAutoSize.LEFT;
			_textField.embedFonts = _isEmbed && _font.hasGlyphs(value);
			_textField.text = value;
			_textField.autoSize = TextFieldAutoSize.NONE;
			
			_textField.width += 2;
			_textField.height;
			
			var bounds:Rectangle = _textField.getCharBoundaries(0);				
			
			if (bounds == null) 
			{
				Looger.error("Error at character creation of " +  (_font != null ? _font.fontName + " " + _font.fontStyle : "") + " U+" + value.charCodeAt().toString(16) + " embedFonts " + _textField.embedFonts);
				_table[value] = null;
				return;
			}
			
			var bmd:BitmapData = new BitmapData(_textField.width, _textField.height, true, _color);
			bmd.draw(_container, _matrix);
			
			var colorRect:Rectangle = bmd.getColorBoundsRect(_mask, _color, false)//.union(bounds);
			
			_table[value] = new GlyphBitmapData(bmd, colorRect, bounds, uint(_textFormat.color));
			
			_textField.text = "";
		}
		
		public function getGlyph(value:String):GlyphBitmapData
		{
			if (_table[value] == undefined) createGlyph(value);		
			return _table[value];			
		}
		
		public function toString():String
		{
			return "[" + getQualifiedClassName(this) + "] font " + _textFormat.font + (_textFormat.bold ? " bold" : "") + (_textFormat.italic ? " italic" : "") + (_textFormat.underline ? " underline" : "") + " - size " + _textFormat.size;
		}
		
		
	}
	
}